﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Besture.Common.Definition;
using Besture.Utils;
using Besture.Common;
using Besture.Bll;
using Besture.Model;

namespace Besture.UI.Email
{
    public partial class Cipher1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.ClientScript.RegisterStartupScript(GetType(), "jsg", "<script>DelaySend();</script>");
            }
            string telphone = Session[Constants.UserMobileCipherSessionKey] + "";
            if (!string.IsNullOrEmpty(telphone) && telphone.Length == 11)
            {
                txtTelphone.Value = telphone;
                labTelphone.Text = telphone.Substring(0, 3) + "******" + telphone.Substring(9, 2);
            }
            else
            {
                Response.Redirect("Cipher.aspx");
            }
        }

        protected void btnFind_Click(object sender, EventArgs e)
        {
            string checkCode = txtVailidCode.Value.Trim();
            if (checkCode.ToUpper() != Session["SMSCode"] + "")
            {
                Fun.Alert(this.Page,"验证码不正确，请重新输入");
                return;
            }
            string telphone = Session[Constants.UserMobileCipherSessionKey] + "";

            UserDac dac = new UserDac();
            UserModel model = dac.GetModel(null, null, telphone);
            if (model == null)
            {
                Fun.Alert(this.Page,"该手机号未注册，请先注册");
                return;
            }

            string pwd = "888888";//(Guid.NewGuid().ToString()).Substring(0, 6);//随机6位密码
            model.Password = Fun.GetMD5(pwd);
            model.UpdateTime = DateTime.Now;
            int re = dac.Update(model);//更新密码
            //发送短信
            string flag = SendSMSUtil.SendSMS(telphone, string.Format(AppConfig.SMSPWDTemplate, pwd));
            if (flag == "发送成功")
            {
                Session[Constants.UserMobileCipherSessionKey] = null;
                Session["SMSCode"] = null;
                Fun.Alert("密码已发送到手机，请查看！", "/Land/Land.aspx");
            }
            else
            {
                Fun.Alert(this.Page,"发送短信失败，请重新发送");
            }
        }
    }
}